#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 8

typedef struct {
  int id;
  char name[16];
  int roomId;
} Student;

typedef struct {
  Student stu[MAXSIZE];
  int length;
} SeqList;

void emptyBlankLine() {
  while (getchar() != '\n') {
    continue;
  }
}

SeqList init(void) {
  SeqList s;
  int num;
  puts("程序初始化运行,请输入初始录入的人数:");
  scanf("%d", &num);
  emptyBlankLine();
  puts("请输入要录取的数据(学号 姓名 房间号):");
  for (s.length = 0; s.length < num; s.length++) {
    scanf("%d %s %d", &s.stu[s.length].id, s.stu[s.length].name,
          &s.stu[s.length].roomId);
    emptyBlankLine();
  }
  return s;
}

void showList(const SeqList *s) {
  int index;
  for (index = 0; index < s->length; index++) {
    printf("学号:%4d 姓名:%6s 房间号:%4d\n", s->stu[index].id,
           s->stu[index].name, s->stu[index].roomId);
  }
}

void showOne(const SeqList *s, int index) {
  printf("学号:%4d 姓名:%6s 房间号:%4d\n", s->stu[index].id, s->stu[index].name,
         s->stu[index].roomId);
}

int findStu(const SeqList *s) {
  char name[16];
  int index;
  scanf("%s", name);
  emptyBlankLine();
  for (index = 0; index < s->length; index++) {
    if (strcmp(s->stu[index].name, name) == 0) {
      return index;
    }
  }
  return -1;
}

void sortList(SeqList *s) {
  int index, preIndex;
  Student current;
  for (index = 1; index < s->length; index++) {
    preIndex = index - 1;
    current = s->stu[index];
    while (preIndex >= 0 && s->stu[preIndex].id > current.id) {
      s->stu[preIndex + 1] = s->stu[preIndex];
      preIndex--;
    }
    s->stu[preIndex + 1] = current;
  }
}

void pushItem(SeqList *s) {
  Student stu;
  puts("请输入要录取的数据(学号 姓名 房间号):");
  if (scanf("%d %s %d", &stu.id, stu.name, &stu.roomId) != 3) {
    puts("输入有误,请检查您的输入");
  }
  emptyBlankLine();
  if (s->length < MAXSIZE) {
    s->stu[s->length] = stu;
    s->length++;
    puts("添加成功");
  } else {
    puts("添加失败,超出数组上限");
  }
}

void deleteItem(SeqList *s) {
  int index;
  printf("根据输入位置删除数据(以1开始):");
  scanf("%d", &index);
  emptyBlankLine();
  if (index > 0 && index <= s->length) {
    index--;
    while (index < s->length - 1) {
      s->stu[index] = s->stu[index + 1];
      index++;
    }
    s->length--;
  } else {
    puts("删除位置有误");
  }
}

int main(void) {
  int choose, index;
  SeqList s = init();
  showList(&s);
  while (!(choose = 0)) {
    puts("请选择您要进行的操作\n"
         "1.查看所有学生数据\t2.按名字查找学生数据\n"
         "3.按学号排序并查看数据\t4.添加学生数据\n"
         "5.删除学生数据\t6.退出");
    scanf("%d", &choose);
    emptyBlankLine();
    switch (choose) {
    case 1:
      showList(&s);
      break;
    case 2:
      printf("输入要查找的用户名:");
      index = findStu(&s);
      if (~index) {
        printf("查找%d次\n", index + 1);
        showOne(&s, index);
      } else {
        puts("未查找到该学生数据");
      }
      break;
    case 3:
      sortList(&s);
      showList(&s);
      break;
    case 4:
      pushItem(&s);
      showList(&s);
      break;
    case 5:
      deleteItem(&s);
      showList(&s);
      break;
    case 6:
      exit(0);
      break;
    default:
      puts("请输入正常的指令");
    }
  }
  return false;
}
